<?php
//系统信息
namespace Admin\Controller;
use Think\Controller;

class CompanyController extends BaseController {

    public function index(){
		$this->display();
	}

	public function config(){
		$this->display();
	}

	public function Company(){
		$this->newid = $_GET['newid'];
		$map['state'] = 1;
		$school = D('Company')->where($map)->order('id desc')->relation(true)->select();
        foreach($school as $k=>$v){
            //将数字的地址转换成文字
            $region = strsToArray($v['location']);
            $regionOne = D("Region")->where("code='".$region[2]."'")->field("fullname")->find();
            $school[$k]['location'] = $regionOne['fullname'];
            if(mb_strlen($regionOne['fullname'],'UTF8') <=9 ){
                $school[$k]['location_text'] = $regionOne['fullname'];
            }else{
                $school[$k]['location_text'] = mb_substr($regionOne['fullname'],0,7,"utf-8")."···";
            }
        }
		$this->school = $school;
        $this->china = D("Region")->getChinaArea();
		$this->display();
		//var_dump($school);
	}

	public function addCompany(){
        $sqldata['location']        = I("location_f_1").",".I("location_s_1").",".I("location_t_1");
		$sqldata['name']        = I("school");
		$sqldata['state']       = 1;
		$sqldata['create_time'] = date("Y-m-d H:i:s");
		$sqldata['update_time'] = date("Y-m-d H:i:s");

		$map['name']  = $sqldata['name'];
		$map['state'] = 1;
		if(M("company")->where($map)->find()){
			$return['state'] = 200;            
	        $return['msg']   = "您已添加了该公司";
			echo json_encode($return);
			exit;
		}

		$result = M("company")->add($sqldata);
		if($result){
			$return['name']  = $sqldata['name'];
			$return['id']    = $result;
			$return['url']   = U('Company/company',array('newid'=>$result));
			$return['state'] = 100;
            $return['msg']   = "添加成功";
        }else{
            $return['state'] = 200;            
            $return['msg']   = "添加失败";
        }
        echo json_encode($return);
        exit;
	}

	public function major(){
		//$map['class'] = 1;
		$map['state'] = 0; // 每公司一个部门只有一个
		$result = M('major')->where($map)->field('id,name')->select();
		foreach ($result as $key => $value) {
			$return[] = $value['name'].'('.$value['id'].')';
		}
		echo json_encode($return);
		exit;
	}

	public function addMajor(){
		$major = I("major");
		$tmp1 = explode('(', $major);
		$major_name = $tmp1[0];
		$tmp2 = explode(')', $tmp1[1]);
		$major_id   = $tmp2[0];

		$map['id']    = $major_id;
		$map['name']  = $major_name;
		//$map['class'] = 1;
		if(M('major')->where($map)->find()){
			$sqldata['state']  = 1;
			$sqldata['company'] = I('school');

			$result = M("major")->where($map)->save($sqldata);
			if($result){
				$return['name']  = $major_name;
				$return['id']    = $major_id;
				$return['state'] = 100;
	            $return['msg']   = "添加成功";
	        }else{
	            $return['state'] = 200;            
	            $return['msg']   = "添加失败";
	        }
	        echo json_encode($return);
	        exit;
		}else{
            $data['name'] = I("major");
            $data['company'] = I("school");
            $data['state']  = 1;
            $tmp1 = explode('(', $major);
            $major_name = $tmp1[0];
            $tmp2 = explode(')', $tmp1[1]);
            $major_id   = $tmp2[0];
            $result = M("major")->add($data);
            if($result){
                $return['name']  = $major_name;
                $return['id']    = $result;
                $return['state'] = 100;
                $return['msg']   = "添加成功";
            }else{
                $return['state'] = 200;
                $return['msg']   = "添加失败";
            }
            echo json_encode($return);
            exit;
		}
	
	}

	public function delMajor(){
		$id = I("id");
		$map['id']  = $id;
		if( 1 ){
			$result = M("major")->where($map)->delete();
			if($result){
				$return['state'] = 100;
	            $return['msg']   = "删除成功";
	        }else{
	            $return['state'] = 200;            
	            $return['msg']   = "删除失败";
	        }
	        echo json_encode($return);
	        exit;
		}else{
			$return['state'] = 200;            
		    $return['msg']   = "数据库错误";
			echo json_encode($return);
			exit;
		}
	
	}

	public function editSchool(){
		$id     = I("id");
		$school = I("school");
		$map['id']    = array('neq',$id);
		$map['name']  = array('eq',$school);
		$map['state'] = 1;

		if(M('company')->where($map)->find()){
			$return['state'] = 200;            
		    $return['msg']   = "已有该公司,公司名不能重复";
			echo json_encode($return);
			exit;
		}else{
			$sqldata['state'] = 1;
			$sqldata['name']  = $school;
			$saveMap['id']    = $id;
			$result = M("company")->where($saveMap)->save($sqldata);
			if($result){
				$return['school'] = $school;
				$return['state'] = 100;
	            $return['msg']   = "保存成功";
	        }else{
	            $return['state'] = 200;            
	            $return['msg']   = "保存失败";
	        }
	        echo json_encode($return);
	        exit;
		}
	
	}

	public function delSchool(){
		$id = I("id");
		$map['id']      = $id;
		$mapM['company'] = $id;
		M('company')->startTrans();
		$hasMajor = M("major")->where($mapM)->select();
		if(empty( $hasMajor ) ){
			$resultMajor = 1;
		}else{
			$resultMajor = M("major")->where($mapM)->delete();
		}
        $resultSchool = M("company")->where($map)->delete();
		if($resultSchool && $resultMajor){
			M('company')->commit();
			$return['state'] = 100;
            $return['msg']   = "删除成功";
        }else{
        	M('company')->rollback();
            $return['state'] = 200;            
            $return['msg']   = "删除失败";
        }
        echo json_encode($return);
        exit;
	}

}